package sketch.specs;

import java.util.List;

import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.internal.core.dom.NaiveASTFlattener;

public class MethodDeclarationDumper extends NaiveASTFlattener {
	
	/**
	 * This class dumps the method body with all unused variables removed
	 * */
	public final List<VariableDeclarationStatement> unusedVars;
	
	public final MethodDeclaration methodToProcess;
	
	public MethodDeclarationDumper(MethodDeclaration method) {
		UnusedVarDeclarationRemover remover = new UnusedVarDeclarationRemover(method.getBody());
		methodToProcess = method;
		unusedVars = remover.getAllRemovableVarDeclarationStatements();
	}
	
	@Override
	public boolean visit(VariableDeclarationStatement statement) {
		if(this.unusedVars.contains(statement)) {
			return false;
		}
		return super.visit(statement);
	}
	
	public String dumpMethodWithUnusedVarRemoved() {
		this.visit(methodToProcess);
		String result = this.getResult();
		return result;
		
	}
}
